// 可选项模式
let someInt: Int? = 42
if case let x? = someInt {
    print(x)
} else {
    print("No value!")
}

// 枚举项模式
if case .some(let x) = someInt {
    print(x)
} else {
    print("No value!")
}
print("-------------------------------")

let nums: [Int?] = [nil, 1, 2, nil, 5]
for case let x? in nums {
    print(x)
}
print("-------------------------------")

// 类型转换模式
protocol Animal {
    var name: String {get}
}

struct Dog: Animal {
    var name: String = "Dog"
    var speed: Int
}

struct Bird: Animal {
    var name: String = "Bird"
    var size: Int
}

struct Fish: Animal {
    var name: String = "Fish"
    var deep: Int
}

let animals: [Any] = [Dog(speed: 50), Bird(size: 20), Fish(deep: 300)]

for animal in animals {
    switch animal {
    case let dog as Dog:
        print("dog speed is \(dog.speed)")
    case let bird as Bird:
        print("bird size is \(bird.size)")
    case is Fish:
        print("blue blue blue...")
    default:
        break
    }
}
print("-------------------------------")

// 表达式模式
struct Teacher {
    var name: String = "Mr.Chan"
    var salary: Int
}

func ~= (pattern: Range<Int>, value: Teacher) -> Bool {
    return pattern.contains(value.salary)
}

let teacher = Teacher(name: "Mr.Ly", salary: 4980)
switch teacher {
case 0..<2000:
    print("温饱线")
case 2000..<5000:
    print("小康")
case 5000..<10000:
    print("美滋滋")
default:
    print("土豪")
}
